package com.gxm.base.jdk8.stream;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.junit.Test;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

public class Demo {

    @Test
    public void filter(){
        List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
        List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
        System.out.println(filtered);
    }

    @Test
    public void test01(){
        List<String> strings = null;
        List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
        System.out.println(filtered);
    }

    @Test
    public void forEach(){
        Random random = new Random();
        random.ints().limit(10).forEach(System.out::println);
    }

    @Test
    public void map(){
        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
        List<Integer> squaresList = numbers.stream().distinct().collect(Collectors.toList());
        System.out.println(squaresList);
        // 获取对应的平方数
        squaresList = numbers.stream().map( i -> i*i).distinct().collect(Collectors.toList());
        // distinct 去重
        System.out.println(squaresList);
    }

    @Test
    public void limit(){
        Random random = new Random();
        random.ints().limit(10).forEach(System.out::println);
    }

    @Test
    public void sorted(){
        Random random = new Random();
        random.ints().limit(10).sorted().forEach(System.out::println);
    }

    @Test
    public void collect(){
        List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
        List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());

        System.out.println("筛选列表: " + filtered);
        String mergedString = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", "));
        System.out.println("合并字符串: " + mergedString);
    }

    @Test
    public void statistics(){
        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);

        IntSummaryStatistics stats = numbers.stream().mapToInt((x) -> x).summaryStatistics();

        System.out.println("列表中最大的数 : " + stats.getMax());
        System.out.println("列表中最小的数 : " + stats.getMin());
        System.out.println("所有数之和 : " + stats.getSum());
        System.out.println("平均数 : " + stats.getAverage());
    }

    /**
     *  7  * contains方法用于判断list集合是否包含某个元素
     *  8  * containsKey方法用于判断Map键中是否包含某个键
     *  9  * containsValue方法用于判断map中是否包含某个value值
     */
    @Test
    public void contains(){
        List list = new ArrayList();
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");
        System.out.println(list.contains("B"));

        Map map = new HashMap();
        map.put("1", "11");
        map.put("2", "22");
        map.put("3", "33");
        map.put("4", "44");
        System.out.println(map.containsKey("1")); //判断map中是否有"1"这个键
        System.out.println(map.containsValue("33")); //判断map中是否有"33"这个值

    }

    /**
     * count方法，跟List接口的size一样，返回的都是这个集合流的元素的长度，不同的是，流是集合的一个高级工厂，中间操作是工厂里的每一道工序，我们对这个流操作完成后，可以进行元素的数量的和；
     */
    @Test
    public void count(){
        List<String> strs = Arrays.asList("a", "a", "a", "a", "b");
        long count = strs.stream().filter(str -> str.equals("a")).count();
        System.out.println(count);
    }

    /**
     * anyMatch表示，判断的条件里，任意一个元素成功，返回true
     */
    @Test
    public void anyMatch(){
        List<String> strs = Arrays.asList("a", "a", "a", "a", "b");
        boolean aa = strs.stream().anyMatch(str -> str.equals("a"));
        System.out.println(aa);
        HashSet<String> hashSet = new HashSet<>();
        hashSet.add("a");
        hashSet.add("a");
        hashSet.add("b");
        hashSet.add("c");
        hashSet.add("a");
        System.out.println(hashSet.stream().collect(Collectors.joining()));
    }

    /**
     * allMatch表示，判断条件里的元素，所有的都是，返回true
     */
    @Test
    public void allMatch(){
        //List<String> strs = Arrays.asList("a", "a", "a", "a", "b");
        List<String> strs = Arrays.asList("a", "a", "a", "a");
        boolean aa = strs.stream().allMatch(str -> str.equals("a"));
        System.out.println(aa);
    }

    /**
     * noneMatch跟allMatch相反，判断条件里的元素，所有的都不是，返回true
     */
    @Test
    public void noneMatch(){
        //List<String> strs = Arrays.asList("a", "a", "a", "a", "b");
        List<String> strs = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "b");
        boolean aa = strs.stream().noneMatch(str -> str.equals("a"));
        System.out.println(aa);
        List<StringBuffer> waypoints = new ArrayList<>();
        int j = 0;
        for (int i = 0; i < strs.size(); i++) {
            if (waypoints.size() == 0 ) {
                waypoints.add(new StringBuffer());
            }
            if (i > 0 && i%3 == 0) {
                j++;
                waypoints.add(new StringBuffer());
            }
            waypoints.get(j).append(strs.get(i)).append(";");
        }
        System.out.println(JSON.toJSONString(waypoints));
    }

    @Test
    public void testIt(){
        List<String> ss = new ArrayList<>();
        ss.add("a");
        ss.add("b");
        ss.add("c");
        Iterator<String> it = ss.iterator();
        while (it.hasNext()) {
            String x = it.next();
            if (x.equals("b")) {
                it.remove();
            }
        }
        System.out.println(ss);
    }


    @Test
    public void fzSorted(){
        SortedBean sortedBean1 = new SortedBean(1, 1, 1);
        SortedBean sortedBean2 = new SortedBean(1, 1, 2);
        SortedBean sortedBean3 = new SortedBean(1, 1, 3);
        SortedBean sortedBean4 = new SortedBean(1, 1, 4);
        SortedBean sortedBean5 = new SortedBean(1, 2, 5);
        SortedBean sortedBean6 = new SortedBean(1, 3, 6);
        SortedBean sortedBean7 = new SortedBean(1, 1, 7);
        SortedBean sortedBean8 = new SortedBean(2, 1, 8);
        SortedBean sortedBean9 = new SortedBean(3, 1, 9);
        List<SortedBean> sortedBeans = Arrays.asList(sortedBean1, sortedBean2, sortedBean3, sortedBean4, sortedBean5, sortedBean6, sortedBean7, sortedBean8, sortedBean9);
        sortedBeans.sort(Comparator.comparing(SortedBean::getKgs).reversed()
                .thenComparingInt(SortedBean::getCbm)
//                .thenComparingInt(SortedBean::getJl).reversed()
        );
        sortedBeans.forEach(System.out::println);
    }

    @Test
    public void test(){
        String strHtml = "<p style=\"text-align: center;\" data-mce-style=\"text-align: center;\">今天适合吃火锅</p><p style=\"text-align: left;\" data-mce-style=\"text-align: left;\">今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅</p><p style=\"text-align: left;\" data-mce-style=\"text-align: left;\"><img class=\"wscnph\" src=\"http://47.97.51.50:8090//pic/ylpt/20230505/202305051507240.jfif\" data-mce-src=\"..//pic/ylpt/20230505/202305051507240.jfif\"></p>";
        strHtml = "<p style=\"text-align: center;\" data-mce-style=\"text-align: center;\">今天适合吃火锅</p><p style=\"text-align: left;\" data-mce-style=\"text-align: left;\">今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃火锅今天适合吃...";
        System.out.println(strHtml.replaceAll("[^\u4E00-\u9FA5]", ""));
    }

    @Test
    public void test011(){
        String card = "410928198603170059";
        System.out.println(card.substring(10, 14));
    }

    @Test
    public void test012(){
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.remove(list.size() -1);
        list.remove(0);
        System.out.println(list);
    }

    @Test
    public void test013(){
        List<SortedBean> beans = new ArrayList<>();
        SortedBean bean1 = new SortedBean(1, 3, 2);
        SortedBean bean2 = new SortedBean(2, 2, 2);
        SortedBean bean3 = new SortedBean(3, 1, 1);
        SortedBean bean4 = new SortedBean(4, 3, 1);
        SortedBean bean5 = new SortedBean(5, 2, 1);
        SortedBean bean6 = new SortedBean(6, 1, 2);
        beans.add(bean1);
        beans.add(bean2);
        beans.add(bean3);
        beans.add(bean4);
        beans.add(bean5);
        beans.add(bean6);
        beans.sort(Comparator.comparing(SortedBean::getKgs)
                .thenComparing(SortedBean::getCbm));
        System.out.println(JSON.toJSONString(beans));
    }

    @Test
    public void test014(){
        List<SortedBean2> bean2s = new ArrayList<>();
        SortedBean2 bean1 = new SortedBean2(1,1, 2, 1);
        SortedBean2 bean2 = new SortedBean2(2,2, 1, 2);
        SortedBean2 bean3 = new SortedBean2(3,3, 1, 1);
        SortedBean2 bean4 = new SortedBean2(4,1, 1, 1);
        bean2s.add(bean1);
        bean2s.add(bean2);
        bean2s.add(bean3);
        bean2s.add(bean4);
        bean2s.sort(Comparator.comparing(SortedBean2::getCbm).reversed()
                .thenComparing(SortedBean2::getKgs).reversed());
        bean2s.forEach(item -> {
            System.out.println(item.toString());
        });
    }
    @Test
    public void test015(){
        double d1 = 0.001D;
        double d2 = 1000D;
        BigDecimal b1 = new BigDecimal(String.valueOf(d1));
        BigDecimal b2 = new BigDecimal(String.valueOf(1000D));
        b1.divide(b2, 6, BigDecimal.ROUND_HALF_UP).doubleValue();
        double d3 = b1.divide(b2, 6, BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.println(d3);
        System.out.println(d1 / d2);
    }

    @Test
    public void test016(){
        double d1 = 0.001D;
        double d2 = 1000D;
        BigDecimal b1 = new BigDecimal(String.valueOf(d1));
        BigDecimal b2 = new BigDecimal(String.valueOf(1000D));
        b1 = b1.divide(b2, 6, BigDecimal.ROUND_HALF_UP);
        System.out.println(b1);
        System.out.println(d1 / d2);
    }

    @Test
    public void test017(){
        double d1 = 0.001D;
        double d2 = 1000D;
        BigDecimal b1 = new BigDecimal(String.valueOf(d1));
        BigDecimal b2 = new BigDecimal(String.valueOf(1000D));
        b1 = b1.divide(b2, 6, BigDecimal.ROUND_HALF_UP);
        System.out.println(b1);
        System.out.println(d1 / d2);
        System.out.println(Double.doubleToLongBits(0) == Double.doubleToLongBits(0.00000));
    }

    @Data
    class SortedBean{
        private int id;
        private int jj;
        private int kgs;
        private int cbm;
        private int sj;

        SortedBean (int id, int kgs, int cbm) {
            this.id = id;
            this.kgs = kgs;
            this.cbm = cbm;
        }
    }

    @Data
    class SortedBean2{
        private int id;
        private int cbm;
        private int kgs;
        private int jl;
        SortedBean2(int id, int cbm, int kgs, int jl) {
            this.id = id;
            this.cbm = cbm;
            this.kgs = kgs;
            this.cbm = cbm;
        }
    }

    @Test
    public void jdds(){
        String a = "100.01";
        System.out.println(Double.valueOf(a) * 100);
    }
}
